<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/GenericDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/NivelDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Usuario.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Nivel.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/utils/System.php'; ?>

<?php

/**
 * Description of UsuarioDAO
 *
 * @author JoasRamos
 */
class UsuarioDAO extends GenericDAO {

    public function __construct() {
        parent::__construct();
    }

    /*
     * Buscar um usuáro pelo id informado.
     */

    public function findById($id) {
        $sql = "SELECT * FROM usuario WHERE id=" . $id;
        $bean = new Usuario();
        $daoNivel = new NivelDAO();
        $nivel = new Nivel();

        $result = mysql_query($sql);

        if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                $bean->setId($row["id"]);
                $bean->setId($row["nome"]);

                //setando nivel
                $nivel = $daoNivel->findById($row["nivel"]);
                $bean->setNivel($nivel);
            }
        }
        System::desconectar();
        return $bean;
    }

    /* Metodo chamado quando o usuário logar no sistema
     * Retornará 0 caso o usuário não seja encontrado.
     */

    public function findUser($user, $pass) {

        $bean = new Usuario();
        $dao = new NivelDAO();
        $nivel = new Nivel();

        $sql = "SELECT * FROM usuario WHERE user = '" . $user . "' AND pass = '" . $pass . "'";

        $result = mysql_query($sql);

        if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                $bean->setId($row["id"]);
                $bean->setNome($row["nome"]);
                $nivel = $dao->findById($row["nivel"]);
                $bean->setNivel($nivel);
            }
        } else {
            $bean->setId(0);
        }
        return $bean;
    }

}
?>
